package com.lihui.demo;

import com.lihui.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2022/12/28  15:56
 */
public class DeleteDemo {
    public static void main(String[] args) throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入学生Id");
            int id = scanner.nextInt();
            connection = DBUtil.getConnection();
            connection.setAutoCommit(false);
             //构造sql语句
            String sql = "delete from student where id = ?";
            //预处理
            statement = connection.prepareStatement(sql);
            //替换占位符为真实值
            statement.setInt(1,id);

            int result = statement.executeUpdate();
            if(result > 0){
                System.out.println("删除学生信息成功");
            }else{
                System.out.println("删除失败");
            }
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            connection.rollback();
        }finally {
            DBUtil.close(null,statement,connection);
        }
    }
}
